package cn.yqn.pet.controller;

import cn.yqn.basic.utils.LoginContext;
import cn.yqn.pet.service.IPetService;
import cn.yqn.pet.domain.Pet;
import cn.yqn.pet.query.PetQuery;
import cn.yqn.basic.utils.JsonResult;
import cn.yqn.basic.utils.PageList;
import cn.yqn.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;

    /**
    * @Title: 上架
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/8/7 9:35
    * @Parameters: [ids]
    * @Return cn.yqn.basic.utils.JsonResult
    */
    @PostMapping("/onsale")
    public JsonResult onSale(@RequestBody List<Long> ids) {
        try {
            petService.onSale(ids);
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.my().setMessage("操作失败,请稍后重试!");
        }
    }

    /**
     * @Title: 下架
     * @Description: TODO
     * @Author: YQN
     * @Version: 1.0
     * @Date:  2022/8/7 9:35
     * @Parameters: [ids]
     * @Return cn.yqn.basic.utils.JsonResult
     */
    @PostMapping("/offsale")
    public JsonResult offSale(@RequestBody List<Long> ids) {
        try {
            petService.offSale(ids);
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.my().setMessage("操作失败,请稍后重试!");
        }
    }

    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public JsonResult saveOrUpdate(@RequestBody Pet pet){
        try {
            if( pet.getId()!=null)
                petService.update(pet);
            else
                petService.save(pet);
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.my().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            petService.remove(id);
            return JsonResult.my();
        } catch (Exception e) {
        e.printStackTrace();
            return JsonResult.my().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id")Long id)
    {
        return petService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Pet> list(HttpServletRequest request){
        // 获取登录人信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        return petService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Pet> queryPage(@RequestBody PetQuery query) {
        try {
            return petService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Pet>();
        }

    }
}
